﻿#ifndef __SS_SNOWFLAKE_H__
#define __SS_SNOWFLAKE_H__
#include <time.h>
#include "../thread/threadLock/ssAtomicLock.h"

#define SS_SNOW_BEGIN_TIMESTAMP 1262275200 //2010-01-01零点时间戳

namespace StarSeeker
{

//4字节时间戳+2字节serverID+2字节自增值
class ssSnowflake
{
public:
	union uuid_u
	{
		long long id;
		int id32[2];
		unsigned short inc[4];
	};

	ssSnowflake();
	~ssSnowflake();

	//初始化
	void Init(unsigned short serverID, int timeStampBegin=SS_SNOW_BEGIN_TIMESTAMP);
	//获取ID
	long long GetID();
private:
	time_t			m_timeStamp;
	unsigned short	m_inc;
	unsigned short	m_serverID;
	time_t			m_timeStampBegin;
};

class ssSafeSnowflake : public ssSnowflake
{
public:
	ssSafeSnowflake();
	~ssSafeSnowflake();

	long long GetSafeID();

private:
	ssAtomicLock	m_lock;
};

}
#endif
